package org.jpro.common.web;

import java.beans.PropertyEditorSupport;
import java.sql.Timestamp;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringEscapeUtils;
import org.apache.commons.lang3.StringUtils;
import org.jpro.common.util.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;

/**
 * 基础控制器
 * 其他控制器继承此控制器获得日期字段类型转换和防止XSS攻击的功能
 */
public class BaseController {

    /**
     * 分页默认显示20条记录
     */
    private static final int DEFAULT_PAGE_SIZE = 20;

    @Autowired
    protected HttpServletRequest request;

    @Autowired
    protected HttpServletResponse response;

    @Autowired
    protected HttpSession session;

    @InitBinder
    public void initBinder(WebDataBinder binder) {
        // String类型转换，将所有传递进来的String进行HTML编码，防止XSS攻击
        binder.registerCustomEditor(String.class, new PropertyEditorSupport() {
            @Override
            public void setAsText(String text) {
                setValue(text == null ? null : StringEscapeUtils.escapeHtml4(text.trim()));
            }

            @Override
            public String getAsText() {
                Object value = getValue();
                return value != null ? value.toString() : "";
            }
        });

        // Date 类型转换
        binder.registerCustomEditor(Date.class, new PropertyEditorSupport() {
            @Override
            public void setAsText(String text) {
                setValue(DateUtils.parseDate(text));
            }
        });

        // Timestamp 类型转换
        binder.registerCustomEditor(Timestamp.class, new PropertyEditorSupport() {
            @Override
            public void setAsText(String text) {
                Date date = DateUtils.parseDate(text);
                setValue(date == null ? null : new Timestamp(date.getTime()));
            }
        });
    }

    /**
     * 获取easyui分页数据
     */
    public Map<String, Object> getEasyUIData(Page page) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("rows", page.getContent());
        map.put("total", page.getTotalElements());
        return map;
    }

    public PageRequest getPageRequest() {
        int pageNo = 0;	// 当前页码
        int pageSize = DEFAULT_PAGE_SIZE;	// 每页行数
        String orderBy = "id";	// 排序字段
        String order = "asc";	// 排序顺序
        if (StringUtils.isNotEmpty(request.getParameter("page"))) {
            pageNo = Integer.valueOf(request.getParameter("page"));

            if (pageNo < 0) {
                pageNo = 0;
            } else if (pageNo > 0) {
                pageNo -= 1;
            }

        }
        if (StringUtils.isNotEmpty(request.getParameter("rows"))) {
            pageSize = Integer.valueOf(request.getParameter("rows"));
        }
        if (StringUtils.isNotEmpty(request.getParameter("sort"))) {
            orderBy = request.getParameter("sort").toString();
        }
        if (StringUtils.isNotEmpty(request.getParameter("order"))) {
            order = request.getParameter("order").toString();
        }

        if (Sort.Direction.ASC.toString().toUpperCase().equals(order)) {
            return new PageRequest(pageNo, pageSize, Sort.Direction.ASC, orderBy.split(","));
        } else {
            return new PageRequest(pageNo, pageSize, Sort.Direction.DESC, orderBy.split(","));
        }

    }

}
